<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属路书" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入所属路书"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属路书名称" prop="parentName">
        <el-input
          v-model="queryParams.parentName"
          placeholder="请输入所属路书名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="笔记" prop="note">
        <el-input
          v-model="queryParams.note"
          placeholder="请输入笔记"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交通方式" prop="method">
        <el-select v-model="queryParams.method" placeholder="请选择交通方式" clearable>
          <el-option
            v-for="dict in map_vehicles_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="道路类型" prop="roadtype">
        <el-select v-model="queryParams.roadtype" placeholder="请选择道路类型" clearable>
          <el-option
            v-for="dict in map_road_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预计时间" prop="predicteTime">
        <el-input
          v-model="queryParams.predicteTime"
          placeholder="请输入预计时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他" prop="other">
        <el-input
          v-model="queryParams.other"
          placeholder="请输入其他"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['work:roadlinks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['work:roadlinks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:roadlinks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Upload"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['work:roadlinks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roadlinksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="所属路书" align="center" prop="parentId" />
      <el-table-column label="所属路书名称" align="center" prop="parentName" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="笔记" align="center" prop="note" />
      <el-table-column label="交通方式" align="center" prop="method">
        <template #default="scope">
          <dict-tag :options="map_vehicles_type" :value="scope.row.method"/>
        </template>
      </el-table-column>
      <el-table-column label="道路类型" align="center" prop="roadtype">
        <template #default="scope">
          <dict-tag :options="map_road_type" :value="scope.row.roadtype"/>
        </template>
      </el-table-column>
      <el-table-column label="预计时间" align="center" prop="predicteTime" />
      <el-table-column label="缩略图" align="center" prop="thumbnail" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.thumbnail" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="其他" align="center" prop="other" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['work:roadlinks:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['work:roadlinks:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" v-model="importopen" width="400px">
      <el-upload
      ref="uploadRef"
      :limit="1"
      accept=".xlsx, .xls"
      :headers="upload.headers"
      :action="upload.url + '?updateSupport=' + upload.updateSupport"
      :disabled="upload.isUploading"
      :on-progress="handleFileUploadProgress"
      :on-success="handleFileSuccess"
      :auto-upload="false"
      drag
      >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处，或
        <em>点击上传</em>
      </div>
      <div class="el-upload__tip" slot="tip">
        <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
        <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
      </div>
      <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFileForm">确 定</el-button>
      <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改路线对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="roadlinksRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属路书" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入所属路书" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="笔记" prop="note">
          <el-input v-model="form.note" placeholder="请输入笔记" />
        </el-form-item>
        <el-form-item label="交通方式" prop="method">
          <el-select v-model="form.method" placeholder="请选择交通方式">
            <el-option
              v-for="dict in map_vehicles_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="道路类型" prop="roadtype">
          <el-select v-model="form.roadtype" placeholder="请选择道路类型">
            <el-option
              v-for="dict in map_road_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预计时间" prop="predicteTime">
          <el-input v-model="form.predicteTime" placeholder="请输入预计时间" />
        </el-form-item>
        <el-form-item label="缩略图" prop="thumbnail">
          <image-upload v-model="form.thumbnail"/>
        </el-form-item>
        <el-form-item label="其他" prop="other">
          <el-input v-model="form.other" placeholder="请输入其他" />
        </el-form-item>
        <el-divider content-position="center">节点信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddMapNodes">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteMapNodes">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="mapNodesList" :row-class-name="rowMapNodesIndex" @selection-change="handleMapNodesSelectionChange" ref="mapNodes">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="线路名称" prop="linkName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.linkName" placeholder="请输入线路名称" />
            </template>
          </el-table-column>
          <el-table-column label="节点名称" prop="nodeName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.nodeName" placeholder="请输入节点名称" />
            </template>
          </el-table-column>
          <el-table-column label="是否线路" prop="isPath" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.isPath" placeholder="请选择是否线路">
                <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="序号" prop="serial" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.serial" placeholder="请输入序号" />
            </template>
          </el-table-column>
          <el-table-column label="笔记" prop="note" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.note" placeholder="请输入笔记" />
            </template>
          </el-table-column>
          <el-table-column label="坐标" prop="position" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.position" placeholder="请输入坐标" />
            </template>
          </el-table-column>
          <el-table-column label="类型" prop="type" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.type" placeholder="请选择类型">
                <el-option
                  v-for="dict in map_node_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="其他" prop="other" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.other" placeholder="请输入其他" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Roadlinks">
import { listRoadlinks, getRoadlinks, delRoadlinks, addRoadlinks, updateRoadlinks } from "@/api/work/roadlinks"
import { getToken } from '@/utils/auth'
const { proxy } = getCurrentInstance()
const { sys_yes_no, map_road_type, map_vehicles_type, map_node_type } = proxy.useDict('sys_yes_no', 'map_road_type', 'map_vehicles_type', 'map_node_type')

const roadlinksList = ref([])
const mapNodesList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedMapNodes = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    parentId: null,
    parentName: null,
    name: null,
    note: null,
    method: null,
    roadtype: null,
    predicteTime: null,
    thumbnail: null,
    other: null
  },
  rules: {
    parentId: [
      { required: true, message: "所属路书不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询路线列表 */
function getList() {
  loading.value = true
  listRoadlinks(queryParams.value).then(response => {
    roadlinksList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: null,
    parentName: null,
    name: null,
    note: null,
    method: null,
    roadtype: null,
    predicteTime: null,
    thumbnail: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    other: null
  }
  mapNodesList.value = []
  proxy.resetForm("roadlinksRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加路线"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRoadlinks(_id).then(response => {
    form.value = response.data
    mapNodesList.value = response.data.mapNodesList
    open.value = true
    title.value = "修改路线"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roadlinksRef"].validate(valid => {
    if (valid) {
      form.value.mapNodesList = mapNodesList.value
      if (form.value.id != null) {
        updateRoadlinks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRoadlinks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除路线编号为"' + _ids + '"的数据项？').then(function() {
    return delRoadlinks(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 节点序号 */
function rowMapNodesIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 节点添加按钮操作 */
function handleAddMapNodes() {
  let obj = {}
  obj.linkName = ""
  obj.nodeName = ""
  obj.isPath = ""
  obj.serial = ""
  obj.note = ""
  obj.position = ""
  obj.type = ""
  obj.remark = ""
  obj.other = ""
  obj.thumbnail = ""
  mapNodesList.value.push(obj)
}

/** 节点删除按钮操作 */
function handleDeleteMapNodes() {
  if (checkedMapNodes.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的节点数据")
  } else {
    const mapNodess = mapNodesList.value
    const checkedMapNodess = checkedMapNodes.value
    mapNodesList.value = mapNodess.filter(function(item) {
      return checkedMapNodess.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handleMapNodesSelectionChange(selection) {
  checkedMapNodes.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('work/roadlinks/export', {
    ...queryParams.value
  }, `roadlinks_${new Date().getTime()}.xlsx`)
}

//导入对话框开启标记
const importopen = ref(false);
const uploadRef = ref()
const upload = reactive({
  // 设置上传的请求头部
  headers: {Authorization: "Bearer " + getToken()},
  updateSupport:0,
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "work/roadlinks/importData",
})

/** 导入按钮操作 */
function handleImport() {
  importopen.value = true;
}

// 文件上传中处理
function handleFileUploadProgress(event, file, fileList) {
  upload.isUploading = true;
}
// 文件上传成功处理
function handleFileSuccess(response, file, fileList) {
  upload.open = false;
  upload.isUploading = false;
  uploadRef.value.clearFiles();
  //this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
  console.log(response.msg, "导入结果", { dangerouslyUseHTMLString: true })
  getList();
}
// 提交上传文件
function submitFileForm() {
  uploadRef.value.submit();
}

getList()
</script>
